package com.jr.controller;


import com.google.gson.Gson;
import com.jr.entity.User;
import com.jr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;


/**
 * 用户信息表
 *
 * @author Snoopy
 * @since 1.0.0 2022-12-31
 */
@Controller
@RequestMapping("/user/")
@SuppressWarnings("all")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * @description:登录功能
     * @author: 宋帅辰
     * @date: 2023/1/2 14:10
     * @param: user, session
     * @return: String, 登录状态(true / false)
     **/
    @PostMapping("login")
    public @ResponseBody
    String loginCheck(User user, HttpSession session) {

        User user1 = userService.checkUser(user);
        Gson gson = new Gson();
        if (user.getUpassword() != null) {
            session.setAttribute("user", user1);
            session.setAttribute("user1", gson.toJson(user1));
        }
        if (user1 != null && (user1.getUname() == null || user1.getUemail() == null || user1.getUphone() == null
                || "".equals(user1.getUname()) || "".equals(user1.getUemail()) || "".equals(user1.getUphone()))) {
            session.setAttribute("flag", "false");
        }
        Boolean bol = user1 != null;
        if (bol) {
            return "true";
        } else {
            return "false";
        }
    }

    /**
     * @description:注册功能
     * @author: 宋帅辰
     * @date: 2023/1/2 14:10
     * @param: user(存储用户名和密码)
     * @return: String, 注册状态 true-成功,exist-用户存在
     **/
    @RequestMapping("register")
    public @ResponseBody
    String registerCheck(User user) {
        user.setUid(String.valueOf(new Date().getTime()));
        if (userService.registerUser(user))
            return "true";
        return "exist";
    }

    /**
     * @description:退出功能，删除域中存储的user
     * @author: 宋帅辰
     * @date: 2023/1/2 14:10
     * @param: session(存储user对象)
     * @return: void
     **/
    @RequestMapping("logout")
    public void registerCheck(HttpSession session) {
        session.removeAttribute("user1");
        session.removeAttribute("user");
        session.removeAttribute("flag");
    }

    /**
     * @description:用户查询功能
     * @author: 宋帅辰
     * @date: 2023/1/2 14:10
     * @param: user
     * @return: user
     **/
    @PostMapping(value = "queryusername", produces = "application/json;charset=utf-8;")
    public @ResponseBody
    User getUserName(User user) {
        User user1 = userService.checkUser(user);
        if (user1 != null)
            return user1;
        return user;
    }

    /**
     * @description:更改密码功能
     * @author: 宋帅辰
     * @date: 2023/1/2 14:10
     * @param: user, session
     * @return: String(更改是否成功)
     **/
    @PostMapping(value = "updatePassword")
    public @ResponseBody
    String updatePassword(User user, HttpSession session) {
        if (userService.changeUserInfo(user, session))
            return "true";
        return "false";
    }

    /**
     * @description:更改用户信息功能
     * @author: 宋帅辰
     * @date: 2023/1/2 14:10
     * @param: user, session
     * @return: String(更改是否成功)
     **/
    @ResponseBody
    @PostMapping(value = "changeUser")
    public String changeUser(User user, HttpSession session) {
        session.removeAttribute("flag");
        User user1 = (User) session.getAttribute("user");
        user.setUid(user1.getUid());
        System.out.println(user);
        boolean success = userService.changeUserInfo(user, session);
        if (success) {
            return "1";
        }
        return "0";
    }
}